
<?php include_once ("../../config.php");?>

<?
//这个用于被其他需要access token的的文件引入inculde的。

//读取数据库，获取公众号的基本资料。appid  appsecret之类的
$wechat_detail=select_one_line_all_items($table_prefix."app_mp","nid > 0");




//配置参数的数组
$CONF =  array(
'__APPID__' =>$wechat_detail['mpapp_id'],
'__SERECT__' =>$wechat_detail['mpapp_secret']
);



//检查token是否已经过期
if($wechat_detail['refresh_token']<date("YmdHis"))
{
echo $wechat_detail['refresh_token'];
echo "<br/>已经超时<br/>";
echo date("YmdHis");




//过期了就要重新拿,没过期就直接提取可以了。
$gettokenUrl  =  "https://api.weixin.qq.com/cgi-bin/token".
"?grant_type=client_credential".
"&appid=" . $CONF['__APPID__'].
"&secret=".$CONF['__SERECT__']
;


$get_json=file_get_contents($gettokenUrl);


echo  "<br/>";
echo $get_json;
echo  "<br/>";
echo  "<br/>";
$access_token            =substr($get_json,17,136);
$token['ai_access_token']=substr($get_json,17,136);
//echo  "<br/>";

echo "现在时间<br/>".$token_expire=date("YmdHis");
echo  "<br/>";
$token_expire=date("YmdHis",strtotime('+1 hour'));
echo $token['ai_refresh_token']=date("YmdHis",strtotime('+1 hour'));
echo  "<br/>";

//过期了就更新一下token和过期时间
echo $update_token=update_date($table_prefix."app_mp",$token,"nid",'1');
    $query = do_query($update_token) or die (mysqli_error().$update_token);
    mysqli_close($con);
}


else
{
    echo "<br/>未超时，直接使用<br/>";
    $access_token=$wechat_detail['access_token'];
    echo "<br/>";

}


//最终输出的是$access_token



?> 


 